<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\admin\model\Admin;
use app\admin\model\Record;
use think\Session;
use think\Db;

/**
 * 项目负责人能看到自己的项目
 *
 * @icon fa fa-circle-o
 */
class Duty extends Backend
{

    /**
     * Project模型对象
     * @var \app\admin\model\Project
     */
    protected $model = null;
    protected $admin_id = null;
    protected $searchFields = 'id,title';
    protected $noNeedRight = ['selectpage', 'index', 'record', 'getRecord', 'admin'];
    protected $dataLimit = 'auth'; //默认基类中为false，表示不启用，可额外使用auth和personal两个值
    protected $dataLimitField = 'user_id'; //数据关联字段,当前控制器对应的模型表中必须存在该字段

    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\Project;
        $this->view->assign("statusList", $this->model->getStatusList());
        $adminpeList = db("admin")->where(["status" => "normal"])->field('id,nickname')->select();
        $this->view->assign("adminpeList", $adminpeList);
        $this->admin_id = Session::get("admin")['id'];
    }


    public function index() {
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total  = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list   = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }

        return $this->view->fetch();
    }

    public function import() {
        parent::import();
    }

    //责任人
    public function admin() {
        $list = Admin::field('nickname,id')->select();
        if ($list) {
            $arr = [];
            foreach ($list as $k => $v) {
                $arr[$v['id']] = $v['nickname'];
            }
            $json = json($arr);
        }
        return $json;
    }


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function searchList() {
        if ($this->admin_id == 1) {
            $where = [];
        } else {

            $where[] = ['exp', Db::raw("FIND_IN_SET($this->admin_id,admin_ids)")];
        }
        $list = $this->model::where($where)->field('title,id')->order('weigh desc')->select();
        if ($list) {
            $arr = [];
            foreach ($list as $k => $v) {
                $arr[$v['id']] = $v['title'];
            }
            $json = json($arr);
        }
        return $json;
    }


    /**
     * Selectpage搜索
     *
     * @internal
     */
    public function selectpage() {
        $params = $this->request->param();

        //如果有默认值就返回
        if (isset($params['searchValue']) && $params['searchValue']) {
            $info = collection($this->model->where(['id' => ['in', $params['searchValue']]])->select())->toArray();
            foreach ($info as $k => $v) {
                $temp          = [];
                $temp['id']    = $v['id'];
                $temp['title'] = $v['title'];
                $temp['name']  = $v['title'];
                $list[]        = $temp;
            }
            return ['list' => $list, 'total' => count($list)];
        }

        return parent::selectpage();
    }


    //项目关联人员
    public function record() {
        $params      = $this->request->param();
        $projectInfo = $this->model->getInfo($params['ids'])[0];
        $adminInfo   = [];
        if ($projectInfo) {
            $admin     = new Admin();
            $adminInfo = $admin->getInfo(null, $projectInfo['admin_ids']);
        }
        $this->view->assign('adminInfo', $adminInfo);
        $this->view->assign('projectId', $params['ids']);
        $this->view->assign('projectName', $projectInfo['title']);
        return $this->view->fetch();
    }


    //获取日志
    public function getRecord() {
        $params     = $this->request->param();
        $record     = new Record();
        $recordInfo = $record->getInfo(null, null, $params['projectId'], $params['adminId']);
        return json(['code' => 1, 'msg' => '获取成功', 'data' => $recordInfo]);
    }

}
